package com.thiny.android.game.thunderbolt.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;

import com.thiny.android.game.thunderbolt.R;
import com.thiny.android.game.thunderbolt.role.GamePlayer;

public class Utils {

	private static final long serialVersionUID = 1L;

	// 保存当前是否开启音乐
	public static void saveMusicStatus(Context context, Boolean isOn) {
		SharedPreferences sp = context.getSharedPreferences(Constants.SP_NAME,
				Context.MODE_PRIVATE);
		sp.edit().putBoolean(Constants.KEY_MUSIC_SETTING, isOn).commit();

	}

	// 获取当前是否开启音乐
	public static boolean isMusicOn(Context context) {
		SharedPreferences sp = context.getSharedPreferences(Constants.SP_NAME,
				Context.MODE_PRIVATE);
		return sp.getBoolean(Constants.KEY_MUSIC_SETTING, false);
	}

	// 获取屏幕长度
	public static int getScreenWidth(Activity activity) {
		return activity.getWindowManager().getDefaultDisplay().getWidth();
	}

	// 获取屏幕高度
	public static int getScreenHeight(Activity activity) {
		return activity.getWindowManager().getDefaultDisplay().getHeight();
	}

	// 使用序列化知识，存储玩家对象list
	public static void stayList(List<GamePlayer> list) {
		try {
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(
					new FileOutputStream(Environment
							.getExternalStorageDirectory().getPath()
							+ "/thunderbolt_db.bin"));
			objectOutputStream.writeObject(list);
			objectOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 使用反序列化知识，获取玩家对象list，并且对该list按一定顺序排序
	public static List<GamePlayer> getList() {
		List<GamePlayer> list = null;
		try {
			ObjectInputStream objectInputStream = new ObjectInputStream(
					new FileInputStream(Environment
							.getExternalStorageDirectory().getPath()
							+ "/thunderbolt_db.bin"));
			list = (List<GamePlayer>) objectInputStream.readObject();
			objectInputStream.close();
		} catch (StreamCorruptedException e) {

			e.printStackTrace();
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		}

		// 给list排序
		if (list != null) {
			Collections.sort(list, new Comparator<GamePlayer>() {

				@Override
				public int compare(GamePlayer player1, GamePlayer player2) {
					long compareScore1 = player1.getScore();
					long compareScore2 = player2.getScore();

					if (compareScore2 > compareScore1) {
						return 1;
					} else if (compareScore1 == compareScore2) {
						return 0;
					} else {
						return -1;
					}
				}

			});
		}

		return list;
	}

	// 实现分享功能:微博，短信等
	public static void StartShareApp(Context context, final long score) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT,
				context.getResources()
						.getString(R.string.share_my_score, score));
		context.startActivity(Intent.createChooser(intent, context
				.getResources().getString(R.string.share)));
	}

}
